package model;

import android.content.ContentValues;
import interfaces.AbstractDrink;
import interfaces.AbstractFood;
import interfaces.AbstractWish;
import database.DatabaseBot;
import drinks.Tee;
import drinks.Water;
import enums.WishType;
import food.BockwurstMitKartoffeln;
import food.ErbsenEintopf;
import food.Griessbrei;
import food.HaferflockenMitMilch;
import food.Cheesburger;
import food.Salat;
import food.Sandwich;
import food.Schnitzel;
import food.Steak;
import wishes.WishToCleanApartment;
import wishes.WishToDrink;
import wishes.WishToEat;
import wishes.WishToPlay;
import wishes.WishToShop;
import wishes.WishToSleep;
import wishes.WishToTakeMedicine;
import wishes.WishToWalk;
import wishes.WishToWashClothes;
import wishes.WishToWashDishes;
import wishes.WishToWatchTV;

public class WishFactory { 

	public static void createRandomWish() {
		int wishNumber = Model.random(1, 8);

		switch(wishNumber) {
		case 1: createWishToEat(); break;
		case 2: createWishToDrink(); break;
		case 3: createWishToTakeRandomMedicine(); break;
		case 4: createWishToSleep(Model.SCHLAFEN_KURZ); break;
		case 5: createWishToWashClothes(); break;
		case 6: createWishToWashDishes(); break;
		case 7: createWishToShop(); break;
		case 8: createWishToCleanApartment(); break;
		}
	}
	public static void createAllWishes() {
		createWishToEat();
		createWishToDrink();
		createWishToTakeRandomMedicine();
		createWishToSleep(Model.SCHLAFEN_KURZ);
		createWishToWashClothes();
		createWishToWashDishes();
		createWishToShop();
		createWishToCleanApartment();
	}
	public static AbstractWish createWishToEat() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToEat)) {
			return null;
		}

		int number = Model.random(1, 9);
		AbstractFood food = null;

		switch (number) {
		case 1: food = new BockwurstMitKartoffeln(); break; 
		case 2: food = new ErbsenEintopf(); break;
		case 3: food = new Griessbrei(); break;
		case 4: food = new HaferflockenMitMilch(); break;
		case 5: food = new Cheesburger(); break;
		case 6: food = new Salat(); break;
		case 7: food = new Sandwich(); break;
		case 8: food = new Schnitzel(); break;
		case 9: food = new Steak(); break;
		}

		AbstractWish wish = new WishToEat(startTime, food);
		Model.getInstance().getWishes().add(wish);

		// Gibt es genung sauberes Geschirr um das Essen zu servieren
		if (!food.hasEnoughDishes()) {
			createWishToWashDishes();
		}
		// Gibt es genung Lebensmitteln im Lager um das Essen zubereiten zu können
		if (!food.hasEnoughIngredients()) {
			createWishToShop();
		}
		return wish;
	}
	public static AbstractWish createWishToDrink() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToDrink)) {
			return null;
		}

		AbstractDrink drink = new Water();
		AbstractWish wish = new WishToDrink(startTime, drink);
		Model.getInstance().getWishes().add(wish);

		// Gibt es genung sauberes Geschirr um das Getränkn zu servieren
		if (!drink.hasEnoughDishes()) {
			createWishToWashDishes();
		}
		// Gibt es im Lager genung Zutaten für dieses Getränk
		if (!drink.hasEnoughIngredients()) {
			createWishToShop();
		}
		return wish;
	}
	public static AbstractWish createWishToSleep(long timeToSleep) {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToSleep)) {
			return null;
		}

		AbstractWish wish = new WishToSleep(startTime, timeToSleep);
		Model.getInstance().getWishes().add(wish);
		if (!wish.canBeFulfilled()) {
			createWishToWashClothes();
		}
		return wish;
	}
	public static AbstractWish createWishToTakeMedicine(String medicine) {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(medicine)) {
			return null;
		}

		AbstractWish wish = new WishToTakeMedicine(startTime, medicine);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}

	public static AbstractWish createWishToTakeRandomMedicine() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToTakeMedicine)) {
			return null;
		}

		int number;
		String medicine = "";
		AbstractWish wish = null;

		while (true) {
			number = Model.random(1, 3);
			if (number == 1) {
				medicine = Model.MEDIZIN_MORGENS;
				if (!Model.getInstance().getWishes().contains(Model.MEDIZIN_MORGENS)) {
					break;
				}
			}
			else if (number == 2) {
				medicine = Model.MEDIZIN_MITTAGS;
				if (!Model.getInstance().getWishes().contains(medicine)) {
					break;
				}
			}
			else {
				medicine = Model.MEDIZIN_ABENDS;
				if (!Model.getInstance().getWishes().contains(medicine)) {
					break;
				}
			}
		}

		wish = new WishToTakeMedicine(startTime, medicine);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToPlay() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToPlay)) {
			return null;
		}

		AbstractWish wish = new WishToPlay(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToWalk() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToWalk)) {
			return null;
		}

		AbstractWish wish = new WishToWalk(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToWatchTV() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToWatchTV)) {
			return null;
		}

		AbstractWish wish = new WishToWatchTV(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToWashClothes() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToWashClothes)) {
			return null;
		}

		AbstractWish wish = new WishToWashClothes(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToWashDishes() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToWashDishes)) {
			return null;
		}
		AbstractWish wish = new WishToWashDishes(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}
	public static AbstractWish createWishToShop() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToShop)) {
			return null;
		}
		AbstractWish wish = new WishToShop(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}

	public static AbstractWish createWishToCleanApartment() {
		long startTime = System.currentTimeMillis();
		if (Model.getInstance().getWishes().contains(WishType.WishToCleanApartment)) {
			return null;
		}

		AbstractWish wish = new WishToCleanApartment(startTime);
		Model.getInstance().getWishes().add(wish);
		return wish;
	}

	public static void createWish(WishType type, long startTime, String extra) {
		AbstractWish wish;

		switch(type) {
		case WishToCleanApartment:
			wish = new WishToCleanApartment(startTime); break;

		case WishToDrink:
			AbstractDrink drink = null;
			if (extra.equals(Model.LEBENSMITTEL_WASSER)) {
				drink = new Water();
			}
			else if (extra.equals(Model.LEBENSMITTEL_TEE)) {
				drink = new Tee();
			}

			wish = new WishToDrink(startTime, drink); break;

		case WishToEat:
			AbstractFood food = null;
			if (extra.equals(Model.SPEISE_BOCKWURST_MIT_KARTOFFELN)) {
				food = new BockwurstMitKartoffeln();
			}
			else if (extra.equals(Model.SPEISE_CHEESBURGER)) {
				food = new Cheesburger();
			}
			else if (extra.equals(Model.SPEISE_ERBSENEINTOPF)) {
				food = new ErbsenEintopf();
			}
			else if (extra.equals(Model.SPEISE_GRIESSBREI)) {
				food = new Griessbrei();
			}
			else if (extra.equals(Model.SPEISE_HAFERFLOCKEN_MIT_MILCH)) {
				food = new HaferflockenMitMilch();
			}
			else if (extra.equals(Model.SPEISE_SALAT)) {
				food = new Salat();
			}
			else if (extra.equals(Model.SPEISE_SANDWICH)) {
				food = new Sandwich();
			}
			else if (extra.equals(Model.SPEISE_SCHNITZEL)) {
				food = new Schnitzel();
			}
			else if (extra.equals(Model.SPEISE_STEAK)) {
				food = new Steak();
			}

			wish = new WishToEat(startTime, food); break;

		case WishToShop:
			wish = new WishToShop(startTime); break;
			
		case WishToSleep:
			wish = new WishToSleep(startTime, Long.valueOf(extra)); break;
			
		case WishToTakeMedicine:
			wish = new WishToTakeMedicine(startTime, extra); break;
			
		case WishToWashClothes:
			wish = new WishToWashClothes(startTime); break;
			
		case WishToWashDishes:
			wish = new WishToWashDishes(startTime); break;
			
		default:
			wish = null; break;
		}

		if (wish != null && !Model.getInstance().getWishes().contains(wish.getType())) {
			Model.getInstance().getWishes().add(wish);
		}
	}
}